package com.zmj.controller;

import com.alibaba.fastjson.JSON;
import com.zmj.entity.User;
import com.zmj.service.LoginService;
import com.zmj.service.impl.LoginServiceImpl;
import com.zmj.utils.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    LoginService loginService=new LoginServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        User user=loginService.login(userName,password);
        Message message=new Message();
        if (user==null){
            message.setCode(1);
            message.setMessage("无此用户");
        }else {
            message.setCode(0);
        }
        resp.getWriter().print(JSON.toJSONString(message));
    }
}
